package com.warehouse.module.system.api.appuser;

import com.warehouse.framework.common.util.object.BeanUtils;
import com.warehouse.module.system.api.appuser.dto.AppUserDTO;
import com.warehouse.module.system.controller.admin.appuser.vo.AppUserSaveReqVO;
import com.warehouse.module.system.dal.dataobject.appuser.AppUserDO;
import com.warehouse.module.system.service.appuser.AppUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AppUserApiImpl implements AppUserApi{
    @Resource
    private AppUserService appUserService;

    @Override
    public AppUserDTO getAppUserByAppIdOpenId(String appId, String openid) {
        AppUserDO userDO = appUserService.getAppUserByAppIdOpenId(appId, openid);
        if(userDO != null) {
            AppUserDTO userDTO = BeanUtils.toBean(userDO, AppUserDTO.class);
            return userDTO;
        }else{
            return null;
        }
    }

    @Override
    public AppUserDTO createAppUser(AppUserDTO appUserDTO) {
        AppUserSaveReqVO userDO = BeanUtils.toBean(appUserDTO, AppUserSaveReqVO.class);
        Long id = appUserService.createAppUser(userDO);
        AppUserDO userDO2 = appUserService.getAppUser(id);
        return BeanUtils.toBean(userDO2, AppUserDTO.class);
    }

    @Override
    public AppUserDTO getByPhoneNo(String appId, String phone){
        AppUserDO userDO2 = appUserService.getAppUserByAppIdPhone(appId, phone);
        return BeanUtils.toBean(userDO2, AppUserDTO.class);
    }

    @Override
    public boolean updateUserPhone(String appId, String openid, String phone){
        return appUserService.updateUserPhone(appId, openid, phone) > 0;
    }

    @Override
    public boolean updateUserOpenId(String appId, String openid, String phone){
        return appUserService.updateUserOpenId(appId, openid, phone) > 0;
    }
}
